/**
 * 显示消息提示框
 * @param content 提示的标题
 */
export function toast(content) {
	uni.showToast({
		icon: 'none',
		title: content
	})
}

/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
export function showConfirm(content) {
	return new Promise((resolve, reject) => {
		uni.showModal({
			title: '提示',
			content: content,
			cancelText: '取消',
			confirmText: '确定',
			success: function(res) {
				resolve(res)
			}
		})
	})
}

/**
 * 参数处理
 * @param params 参数
 */
export function tansParams(params) {
	let result = ''
	for (const propName of Object.keys(params)) {
		const value = params[propName]
		var part = encodeURIComponent(propName) + "="
		if (value !== null && value !== "" && typeof(value) !== "undefined") {
			if (typeof value === 'object') {
				for (const key of Object.keys(value)) {
					if (value[key] !== null && value[key] !== "" && typeof(value[key]) !== 'undefined') {
						let params = propName + '[' + key + ']'
						var subPart = encodeURIComponent(params) + "="
						result += subPart + encodeURIComponent(value[key]) + "&"
					}
				}
			} else {
				result += part + encodeURIComponent(value) + "&"
			}
		}
	}
	return result
}


/**
 * 构造树型结构数据
 * @param {*} data 数据源
 * @param {*} id id字段 默认 'id'
 * @param {*} parentId 父节点字段 默认 'parentId'
 * @param {*} children 孩子节点字段 默认 'children'
 * @param {*} rootId 根Id 默认 0
 */
export function handleTree(data, rootId, id, parentId, children) {
	id = id || "id";
	parentId = parentId || "parentId";
	children = children || "children";
	rootId = rootId || "0";

	// 对源数据深度克隆
	const cloneData = JSON.parse(JSON.stringify(data));
	// 循环所有项
	const treeData = cloneData.filter((father) => {
		const branchArr = cloneData.filter((child) => {
			// 返回每一项的子级数组
			return father[id] === child[parentId];
		});
		branchArr.length > 0 ? (father[children] = branchArr) : "";
		// 返回第一层
		return father[parentId] === rootId;
	});
	return treeData !== "" ? treeData : data;
}

export const getObjType = (obj) => {
	const toString = Object.prototype.toString;
	const map = {
		"[object Boolean]": "boolean",
		"[object Number]": "number",
		"[object String]": "string",
		"[object Function]": "function",
		"[object Array]": "array",
		"[object Date]": "date",
		"[object RegExp]": "regExp",
		"[object Undefined]": "undefined",
		"[object Null]": "null",
		"[object Object]": "object",
	};
	return map[toString.call(obj)];
};

/**
 * 对象深拷贝
 */
export const deepClone = (data) => {
	const type = getObjType(data);
	let obj;
	if (type === "array") {
		obj = [];
	} else if (type === "object") {
		obj = {};
	} else {
		// 不再具有下一层次
		return data;
	}
	if (type === "array") {
		let i = 0;
		const len = data.length;
		for (; i < len; i++) {
			obj.push(deepClone(data[i]));
		}
	} else if (type === "object") {
		for (const key in data) {
			obj[key] = deepClone(data[key]);
		}
	}

	return obj;
};

/**
 * 判断是否为空
 */
export function isNull(val) {
	const objType = getObjType(val);
	if (objType === "boolean") {
		return false;
	}
	if (objType === "number") {
		return false;
	}
	if (objType === "array") {
		if (val.length === 0) return true;
	} else if (objType === "object") {
		if (JSON.stringify(val) === "{}") {
			return true;
		}
	} else {
		if (val === "null" || val == null || val === "undefined" || val === undefined || val === "")
			return true;
		return false;
	}
	return false;
}

/**
 * 判断是否不为空
 */
export function isNotNull(val) {
	return !isNull(val);
}

/**
 * 日期格式化 默认格式
 */
export function dateFormat(date) {
	return dateFormatWithFmt(date);
}

/**
 * 日期格式化 自定义格式
 */
export function dateFormatWithFmt(date, fmt) {
	let format = fmt || "yyyy-MM-dd";

	if (date !== "Invalid Date") {
		const o = {
			"M+": date.getMonth() + 1, // month
			"d+": date.getDate(), // day
			"h+": date.getHours(), // hour
			"m+": date.getMinutes(), // minute
			"s+": date.getSeconds(), // second
			"q+": Math.floor((date.getMonth() + 3) / 3), // quarter
			S: date.getMilliseconds() // millisecond
		};
		if (/(y+)/.test(format)) {
			format = format.replace(
				RegExp.$1,
				(date.getFullYear() + "").substr(4 - RegExp.$1.length)
			);
		}
		for (const k in o) {
			if (new RegExp("(" + k + ")").test(format)) {
				format = format.replace(
					RegExp.$1,
					RegExp.$1.length === 1 ?
					o[k] :
					("00" + o[k]).substr(("" + o[k]).length)
				);
			}
		}
		return format;
	}
	return "";
}


export function getTimeDuring(date) {
	const thisDate = new Date(date)
	const month = thisDate.getMonth() + 1;
	const day = thisDate.getDate();
	if (day > 10) {
		if (day > 20) {
			return month + "月下旬";
		}
		return month + "月中旬";
	}
	return month + "月上旬";
}

export function addDay(date, days) {
	let thisDate = new Date(date);
	thisDate.setDate(thisDate.getDate() + days)
	let year = thisDate.getFullYear();
	//补全位数
	let month = (thisDate.getMonth() + 1).toString().padStart(2, '0');
	let day = (thisDate.getDate()).toString().padStart(2, '0');
	return `${year}-${month}-${day}`;

}